//
//  XCSMSSelectCountryView.m
//  HOLLASMSLoginKit
//
//  Created by Mars on 2019/10/10.
//  Copyright © 2019 mao PengLin. All rights reserved.
//

#import "XCSMSPhoneNumberTextField.h"
#import "Config.h"
#import "XCSMSTheme.h"

@interface XCSMSPhoneNumberTextField()
{
    UIView *nationalFlagBackGroundView;     //国旗区号背景视图
    UILabel *nationalFlagView;      //国旗
    UILabel *countryNumLab;     //区号
    UIImageView *arrowView;     //箭头
    UIView *verticalLine;          //竖分割线
    UIView *separatorLine;      //分割线
}
@end

@implementation XCSMSPhoneNumberTextField

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self setViewLayout];
    }
    return self;
}

- (void)setupUI{
    
    nationalFlagBackGroundView = [[UIView alloc] init];
    [self addSubview:nationalFlagBackGroundView];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nationalFlagViewClick:)];
    [nationalFlagBackGroundView addGestureRecognizer:tapGes];
    
    nationalFlagView = [[UILabel alloc] init];
    nationalFlagView.font = [UIFont systemFontOfSize:18];
    [nationalFlagBackGroundView addSubview:nationalFlagView];
    
    countryNumLab = [[UILabel alloc] init];
    [countryNumLab setFont:MediumFont(18)];
    [countryNumLab setTextColor:[XCSMSTheme sharedTheme].inputTextColor];
    [countryNumLab setText:@"+1"];
    [countryNumLab setTextAlignment:NSTextAlignmentCenter];
    [nationalFlagBackGroundView addSubview:countryNumLab];
    
    arrowView = [[UIImageView alloc] init];
    arrowView.contentMode = UIViewContentModeScaleAspectFit;
    [arrowView setImage:[UIImage imageNamed:@"CAMPROSMSKit.bundle/campro_assets/campro_arrow_down_black.png"]];
    
    [nationalFlagBackGroundView addSubview:arrowView];
    
    verticalLine = [[UIView alloc] init];
    [verticalLine setBackgroundColor:[XCSMSTheme sharedTheme].verticalSeparatorColor];
    [self addSubview:verticalLine];
    
    separatorLine = [[UIView alloc] init];
    [separatorLine setBackgroundColor:[XCSMSTheme sharedTheme].horizontalSeparatorColor];
    [self addSubview:separatorLine];
    
    self.phoneTextField = [[XCSMSTextField alloc] init];
    self.phoneTextField.tintColor = [XCSMSTheme sharedTheme].inputCursorColor;
    self.phoneTextField.textColor = [XCSMSTheme sharedTheme].inputTextColor;
    self.phoneTextField.font = MediumFont(18);
    if (@available(iOS 10.0, *)) {
        self.phoneTextField.textContentType = nil;
    }
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.phoneTextField becomeFirstResponder];
    [self addSubview:self.phoneTextField];
    
}

- (void)setViewLayout{
    if (SMSIsArabic()) {
        nationalFlagView.frame = CGRectMake(4, 0, 24, 48);
        countryNumLab.frame = CGRectMake(nationalFlagView.frame.size.width + 8, 0, countryNumLab.text.length * 12, 48);
        arrowView.frame = CGRectMake(countryNumLab.frame.size.width + countryNumLab.frame.origin.x + 8, 0, 10, 48);
        CGFloat bgWidth = arrowView.frame.origin.x + arrowView.frame.size.width;
        nationalFlagBackGroundView.frame = CGRectMake(self.frame.size.width - bgWidth, 0, bgWidth, 48);
        
        verticalLine.frame = CGRectMake(nationalFlagBackGroundView.frame.origin.x - 8, (48 - 16)/2, 1, 16);
        separatorLine.frame = CGRectMake(0, 46, HWScreenWidth - 88, 1);
        self.phoneTextField.frame = CGRectMake(0, 0, verticalLine.frame.origin.x - 8, 48);
        self.phoneTextField.textAlignment = NSTextAlignmentRight;
    } else {
        nationalFlagView.frame = CGRectMake(4, 0, 24, 48);
        countryNumLab.frame = CGRectMake(nationalFlagView.frame.size.width + 8, 0, countryNumLab.text.length * 12, 48);
        arrowView.frame = CGRectMake(countryNumLab.frame.size.width + countryNumLab.frame.origin.x + 8, 0, 10, 48);
        nationalFlagBackGroundView.frame = CGRectMake(0, 0, arrowView.frame.origin.x + arrowView.frame.size.width + 8, 48);
        
        verticalLine.frame = CGRectMake(nationalFlagBackGroundView.frame.origin.x + nationalFlagBackGroundView.frame.size.width, (48 - 16)/2, 1, 16);
        separatorLine.frame = CGRectMake(0, 46, HWScreenWidth - 88, 1);
        self.phoneTextField.frame = CGRectMake(verticalLine.frame.origin.x + 8, 0, separatorLine.frame.size.width - verticalLine.frame.origin.x, 48);
    }
}


- (void)setCountry:(XCSMSISOCountry *)model {
    
    self.countryModel = model;
    nationalFlagView.text = model.countryFlag;
    countryNumLab.text = model.countryCode;
    
    [self setViewLayout];
}

#pragma mark - UITapGestureRecognizer
- (void)nationalFlagViewClick:(UITapGestureRecognizer *)tap {
    if (self.nationClickBlock){
        self.nationClickBlock();
    }
}

#pragma mark - textField
- (void)textFieldChanged:(UITextField*)textField{
    if (self.textFieldChangeBlock){
        self.textFieldChangeBlock(textField);
    }
}

@end
